package com.ruix.app.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruix.app.R;

/**
 * Created by hadexs on 2017/5/18.
 */

public class SumupItemView extends LinearLayout{

    private  EditText editText;
    private DeleteTargeListener listener;

    public SumupItemView(Context context) {
        super(context);
        initView(context);
    }

    public SumupItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public SumupItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context){
        LayoutInflater.from(context).inflate(R.layout.item_lesson_sumup,this);
        editText = (EditText) findViewById(R.id.edite_target);
        editText.requestFocus();
        findViewById(R.id.iv_delete_target).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    listener.removeView(SumupItemView.this);
                }

            }
        });
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
//                if (listener != null){
//                    listener.afterTextChanged(s.length() > 0);
//                }
            }
        });
    }


    public String getInputValue(){
        return editText.getText().toString().trim();
    }

    public void clearInput(){
        editText.setText("");

    }

    public void setListener(DeleteTargeListener listener) {
        this.listener = listener;
    }

     public interface  DeleteTargeListener{
        void removeView(View view);
        void afterTextChanged(boolean hasValue);
    }





}
